﻿<cfcomponent>
<cfscript>
DB = 'chinalaohu';
dbt = 'user_base';
</cfscript>
  <cffunction name="login" access="remote" returntype="any" returnFormat="json">
  <cfscript>
	success = 0;
	rs = {"success": success};
	lhid = 0;
	
	

		/*userpwd='28f673f31cdd6af50d1f0b8e2b71b9e5';
		screenw=720;
		sdkver=16;
		clientplat='andr';
		imsi='460020026758929';
		screenh=1280;
		okey='9838449bf51571d660f918fd1ebd8db5';
		sourceacc='ChinaTiger';
		mobile='568565565';
		sid = 66407086;
		vcode = '1234';*/


//{userpwd=28f673f31cdd6af50d1f0b8e2b71b9e5, sid=25639525, screenw=720, sdkver=16, clientplat=andr, imsi=460020026758929, screenh=1280, okey=b5ab7bea9febb1ea9da5a2687c18e99c, vcode=1234, sourceacc=ChinaTiger, mobile=44666255}
  </cfscript>
  <cfif isdefined('mobile')>
  	<!---<cfif isdefined('sid')--->
  
  
  
		<cfscript>
		nkey = hash(	mobile & imsi & clientplat & sourceacc & dateformat(now(),'yyyymmdd') );
    </cfscript>
    <!---<cfif okey eq nkey>--->
    
    <!---SID过来，根据SID查出错次数，超过3次就判断VCODE是否和数据库里的VCODE相同--->
    
    
    <!---查找是否有SID并且长度大于0--->
    <cfif isdefined('sid') and len(sid) gt 0>
    	<!---查找SID在验证码库中的记录--->
      <cfquery datasource="#DB#" name="checkvcode" >
      select counts
      from user_vcode
      where sid=#sid#
      </cfquery>
      <!---SID在库中有记录--->
      <cfif checkvcode.recordcount gt 0>
      	<!---是否超过3次--->
        <cfif checkvcode.counts gte 3>
        	<!---验证码是否相等--->
        	<cfquery datasource="#DB#" name="checkvcode">
          select * from user_vcode
          where sid=#sid#
          </cfquery>
        	<!---验证码不相等--->
          <cfif checkvcode.vcode neq vcode>
          
          
          	<cfquery datasource="#DB#">
            update user_vcode set
            vcode = '#getRandom()#',
            counts = counts + 1
            where sid=#sid#
            </cfquery>          
            <cfset rs = {"success": 2, "sid": sid, "errorcounts":checkvcode.counts+1}>
          <!---验证码相等--->
          <cfelse>
          	<!---相等验证KEY--->
            
            <cfif okey eq nkey>
              <cfinvoke method="checkpwd" mobile="#mobile#" userpwd="#userpwd#" sid="#sid#" returnvariable="rs" />
            <cfelse>
            	<cfscript>
							rs = {"success": success, "sid": sid, "msg":"有SID后KEY不等"};
							</cfscript>
            </cfif>
            
          </cfif>
        <!---没有超过3次--->
        <cfelse>
        	<cfquery datasource="#DB#">
          update user_vcode set
          vcode = '#getRandom()#',
          counts = counts + 1
          where sid=#sid#
          </cfquery>
					<cfscript>
          rs = {"success": success, "sid": sid, "errorcounts":checkvcode.counts+1, "msg":"没有超过3次"};
          </cfscript>
          
          
        </cfif>
      <!---SID在库中没有记录--->
      <cfelse>
				<cfscript>
        rs = {"success": 2, "msg":"SID在库中没有记录"};
        </cfscript>
      </cfif>
    <!---没有SID--->
    <cfelse>
      <!---相等验证KEY--->
      <cfif okey eq nkey>      
        <cfinvoke method="checkpwd" mobile="#mobile#" userpwd="#userpwd#" returnvariable="rs" />
      <cfelse>
				<cfscript>
        rs = {"success": success, "msg":"无ID后KEY不等"};
        </cfscript>
      </cfif>
    </cfif>
    <!---</cfif>--->
  	<!---登录记录--->
    <!---<cfquery datasource="#DB#">
    insert into user_platform(uid,mobile,userpwd,sourceAcc,IMSI,clientPlat,SDKver,screenH,screenW,platKEY,logindt,success)
    value(#lhid#,'#mobile#','#userpwd#','#sourceacc#','#imsi#','#clientplat#','#sdkver#','#screenh#','#screenw#','#okey#',#now()#,#success#)
    </cfquery>--->
  </cfif>
		<cfreturn rs>
	</cffunction>
  
  
  <cffunction access="remote" name="checkkey" returntype="any">
  
      <cfinvoke method="checkpwd" mobile="#mobile#" userpwd="#userpwd#" sid="#sid#" returnvariable="rs" />
      
      
  </cffunction>
  
  
	<cffunction access="remote" name="checkpwd" returntype="any">
    <cfquery datasource="#DB#" name="getuid">
    select uid
    from #dbt#
    where mobile='#mobile#' and userpwd = '#userpwd#'
    </cfquery>
		<cfscript>
    if (getuid.recordcount gt 0){
      //成功
      success = 1;
      lhid = getuid.uid;
      rs = {"lhid":lhid,"success": success};
      
    }
    else {
      //出错，生成SID
      if (not isdefined('sid')){
        sid = RandRange(1,99999999);
      }
      rs = {"sid":sid,"success": 0};
    }
    </cfscript>
    
    <cfif isdefined('sid')>
			<cfif getuid.recordcount eq 0>
        <cfinvoke component="cfc.plat.user_vcode" method="errorlog" returnvariable="errorlogcount" sid="#sid#"></cfinvoke>
        <cfset rs = {"sid":sid,"success":0,"errorcounts":errorlogcount,"msg":"验证密码出错"}>
        <cfif errorlogcount gte 3>
            <cfset newvcode = getRandom()>
            <cfquery datasource="#DB#">
            update user_vcode set
            vcode = '#newvcode#'
            where sid=#sid#
            </cfquery>
          <!---<cfinvoke component="cfc.plat.user_vcode" method="modvcode" sid="#sid#" returnvariable="newvcode" ></cfinvoke>--->
          <cfinvoke method="newvcodeimg" sid="#sid#" newvcode="#newvcode#"></cfinvoke>
        </cfif>
      <cfelse>
        <!---删除SID--->
        <cfquery datasource="#DB#">
        delete from user_vcode
        where sid = #sid#
        </cfquery>
        <cfscript>
				captchaimagepath = getdirectoryfrompath(getcurrenttemplatepath()) & 'newcaptcha' & sid & '.png';
				FileDelete(captchaimagepath);
				</cfscript>
      </cfif>
    </cfif>
    <cfreturn rs>
  </cffunction>
  
  <cfscript>
	function getRandom(){
		var rStr = "";
		for(i=0;i<4;i=i+1){
			rStr = rStr & Chr(RandRange(Asc("0"),Asc("9")));
		};
		return rStr;
  }
  </cfscript>
  
  <!---验证码--->
  <cffunction name="refreshvcode" access="remote" returnFormat="json">
  	<!---记录次数和生成随机码--->
    <cfinvoke component="cfc.plat.user_vcode" method="modvcode" sid="#sid#" returnvariable="newvcode" ></cfinvoke>
    <cfinvoke method="newvcodeimg" sid="#sid#" newvcode="#newvcode#"></cfinvoke>
    <cfreturn {vcodefile:'newcaptcha' & sid & '.png'}>
    
  </cffunction>
  
  <cffunction name="newvcodeimg" access="remote">
    <cfscript>
		captchaimagepath = getdirectoryfrompath(getcurrenttemplatepath()) & 'newcaptcha' & sid & '.png';
    </cfscript>
    <cfimage action="captcha" width="88" height="40" text="#newvcode#" destination="#captchaimagepath#" difficulty="medium" fontSize="20" overwrite="yes">
    <!---<cfcontent reset="no" type="image/png" file="#captchaimagepath#" deletefile="no">--->
  </cffunction>
  
  <cffunction name="MobileGetRegvcode" access="remote" returntype="any" returnFormat="json">
  	<cfscript>
		success = 0;
		rs = {"success": success};
		</cfscript>
    <!---判断手机号是否注册--->
    <cfquery datasource="#DB#" name="checkmobile">
    select uid
    from #dbt#
    where mobile = '#mobile#'
    </cfquery>
		<cfif checkmobile.recordcount eq 0>
    	<cfscript>
			success = 1;
			rs = {"success": success};
			</cfscript>
      <cfinvoke component="cfc.plat.user_regvcode" method="createvcode" mobile="#mobile#" />
      
		</cfif>
		<cfreturn rs>
  
  </cffunction>
  
<cffunction access="remote" name="getnewvcode" returnFormat="json">
<!---刷新注册验证码--->
	<cfscript>
  //mobile = '13402679066';
  success = 0;
  rs = {"success": success};
  </cfscript>
  <!---判断手机号是否注册--->
  <cfquery datasource="#DB#" name="checkmobile">
  select uid
  from #dbt#
  where mobile = '#mobile#'
  </cfquery>
  <cfif checkmobile.recordcount eq 0>
  	<cfinvoke component="cfc.plat.user_regvcode" method="createvcode" mobile="#mobile#" />
		<cfscript>
    success = 1;
    rs = {"success": success};
    </cfscript>
  </cfif>
  <cfreturn rs>
</cffunction>

<cffunction access="remote" name="vcodeReg" returnFormat="json">
	<cfscript>
  mobile = '13402679077';
  success = 0;
  rs = {"success": success};
  </cfscript>
  <!---判断手机号是否注册--->
  <cfquery datasource="#DB#" name="checkmobile">
  select uid
  from #dbt#
  where mobile = '#mobile#'
  </cfquery>
  <cfif checkmobile.recordcount eq 0>
  	<!---验证码输入--->
    <cfinvoke component="cfc.plat.user_regvcode" method="checkvcode" mobile="#mobile#" returnvariable="checkvcode" />
    <cfif checkvcode.recordcount eq 0>
    	<!---验证码不存在或已经过期--->
			<cfscript>
			success = 2;
			rs = {"success": success};
      </cfscript>
    <cfelseif vcode neq checkvcode.vcode>
    	<cfif checkvcode.counts gte 3>
				<cfscript>
        success = 3;
        rs = {"success": success};
        </cfscript>
        <cfquery datasource="#DB#">
        delete from user_regvcode
        where mobile = '#mobile#'
        </cfquery>
      <cfelse>
        <cfquery datasource="#DB#">
        update user_regvcode set
        counts = counts + 1
        where mobile = '#mobile#'
        </cfquery>
				<cfscript>
        success = 0;
        rs = {"success": success};
        </cfscript>
      </cfif>
    <cfelse>
      <!---正式注册--->
      <cfquery datasource="#DB#" result="addrs">
      insert into #dbt#(create_dt,mobile)
      values(#now()#,'#mobile#')
      </cfquery>
			<cfscript>
      success = 1;
      rs = {"success": success, "lhid": addrs.GENERATEDKEY};
      </cfscript>
    </cfif>
  <cfelse>
		<cfscript>
    success = 0;
    rs = {"success": success, "lhid":checkmobile.uid};
    </cfscript>
  </cfif>
  <cfreturn rs>
</cffunction>
  
</cfcomponent>

